package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;



    @Override
    @Transactional
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        if(queryString!=null && queryString.length()>0){
            if (queryString.equals("一级")){
                queryString = "1";
            }

            if (queryString.equals("二级")){
                queryString = "2";
            }
        }


        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page =menuDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Menu> result = page.getResult();
        return new PageResult(total,result);

    }

    @Override
    @Transactional
    public List<Menu> findFirstMenu() {
        return menuDao.findFirstMenu();
    }

    @Override
    public void add(Menu menu,Integer[] roleIds) {
        //设置level
        if (menu.getParentMenuId()==null){
            menu.setLevel(1);
        }else{
            menu.setLevel(2);
        }

        List<Menu> menu1 = menuDao.findByMenu(menu);

        if (menu1!=null && menu1.size()>0){

            throw new RuntimeException("菜单信息重复，请重新输入");
        }else{
            menuDao.add(menu);
        }
        if (roleIds.length>0){
            for (Integer roleId : roleIds) {
                menuDao.addRoleMenu(menu.getId(),roleId);
            }
        }
    }

    @Override
    @Transactional
    public Menu findById(Integer menuId) {
        return menuDao.findById(menuId);
    }

    @Override
    @Transactional
    public void edit(Menu menu) {
        //设置level
        if (menu.getParentMenuId()==null){
            menu.setLevel(1);
        }else{
            menu.setLevel(2);
        }

        List<Menu> menu1 = menuDao.findByMenu(menu);

        if (menu1.size()>1){
            throw new RuntimeException("菜单信息重复，请重新输入");
        }else{
            menuDao.edit(menu);
        }
    }

    @Override
    @Transactional
    public void deleteById(Integer menuId) {
        int count = menuDao.findCountRoleMenuByMenuId(menuId);
        if (count!=0){
            throw new RuntimeException("与角色信息关联，不予删除");
        }else{
            menuDao.deleteById(menuId);
        }

    }

    @Override
    @Transactional
    public Map getMenu(String username) {
        /*根据登录用户名查询用户id*/
        Integer id = userDao.findUserByUsername(username).getId();
        /*根据用户id查询关联的menuId数组*/
        Integer[] menuIdList = menuDao.findMenuIdByUserId(id);
        /*根据menuId查询对应的:一级目录*/
        List<Menu> menuList = menuDao.getMenu(menuIdList);
        Map map = new HashMap();
        /*遍历一级目录分别查询对应的:二级目录*/
        if (menuList != null && menuList.size() > 0) {
            for (Menu menu : menuList) {
                menu.setChildren(menuDao.getchildren(menu.getId(), menuIdList));
            }
            map.put("menuList", menuList);
            List<Menu> children = menuList.get(0).getChildren();
            if (children != null) {
                String linkUrl = children.get(0).getLinkUrl();
                map.put("linkUrl", linkUrl);
                System.out.println(linkUrl);
            }
        }
        return map;
    }

    @Override
    @Transactional
    public List<Role> findAllRoles() {
        return roleDao.findAll();
    }


}
